@model CategorySearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.Catalog.Categories").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Categories");
}

@{
    const string hideSearchBlockAttributeName = "CategoryPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<div class="content-header clearfix">
    <h1 class="float-left">
        @T("Admin.Catalog.Categories")
    </h1>
    <div class="float-right">
        <a asp-action="Create" class="btn btn-primary">
            <i class="fas fa-square-plus"></i>
            @T("Admin.Common.AddNew")
        </a>
        <div class="btn-group">
            <button type="button" class="btn btn-success">
                <i class="fas fa-download"></i>
                @T("Admin.Common.Export")
            </button>
            <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                <span class="caret"></span>
                <span class="sr-only">&nbsp;</span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li class="dropdown-item">
                    <a asp-action="ExportXml">
                        <i class="far fa-file-code"></i>
                        @T("Admin.Common.ExportToXml")
                    </a>
                </li>
                <li class="dropdown-item">
                    <a asp-action="ExportXlsx">
                        <i class="far fa-file-excel"></i>
                        @T("Admin.Common.ExportToExcel")
                    </a>
                </li>
            </ul>
        </div>
        <button type="button" name="importexcel" class="btn bg-olive" data-toggle="modal" data-target="#importexcel-window">
            <i class="fas fa-upload"></i>
            @T("Admin.Common.Import")
        </button>
        @await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.CategoryListButtons, additionalData = Model })
        <button type="button" id="delete-selected" class="btn btn-danger">
            <i class="far fa-trash-can"></i>
            @T("Admin.Common.Delete.Selected")
        </button>
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <div class="form-horizontal">
            <div class="cards-group">
                <div class="card card-default card-search">
                    <div class="card-body">
                        <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                            <div class="search-text">@T("Admin.Common.Search")</div>
                            <div class="icon-search"><i class="fas fa-magnifying-glass" aria-hidden="true"></i></div>
                            <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                        </div>

                        <div class="search-body @(hideSearchBlock ? "closed" : "")">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchCategoryName" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchCategoryName" />
                                        </div>
                                    </div>
                                    <div class="form-group row" @(Model.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchStoreId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchPublishedId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchPublishedId" asp-items="Model.AvailablePublishedOptions" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="text-center col-12">
                                    <button type="button" id="search-categories" class="btn btn-primary btn-search"><i class="fas fa-magnifying-glass"></i>@T("Admin.Common.Search")</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card card-default">
                    <div class="card-body">
                        <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.Categories", Docs.Categories + Utm.OnAdmin)" />

                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "categories-grid",
                            UrlRead = new DataUrl("List", "Category", null),
                            SearchButtonId = "search-categories",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.SearchCategoryName)),
                                new FilterParameter(nameof(Model.SearchStoreId)),
                                new FilterParameter(nameof(Model.SearchPublishedId))
                            },
                            ColumnCollection = new List<ColumnProperty>
                            {
                                 new ColumnProperty(nameof(CategoryModel.Id))
                                {
                                    IsMasterCheckBox = true,
                                    Render = new RenderCheckBox("checkbox_categories"),
                                    ClassName =  NopColumnClassDefaults.CenterAll,
                                    Width = "50"
                                },
                                new ColumnProperty(nameof(CategoryModel.Breadcrumb))
                                {
                                    Title = T("Admin.Catalog.Categories.Fields.Name").Text
                                },
                                new ColumnProperty(nameof(CategoryModel.Published))
                                {
                                    Title = T("Admin.Catalog.Categories.Fields.Published").Text,
                                    Width = "100",
                                    ClassName = NopColumnClassDefaults.CenterAll,
                                    Render = new RenderBoolean()
                                },
                                new ColumnProperty(nameof(CategoryModel.DisplayOrder))
                                {
                                    Title = T("Admin.Catalog.Categories.Fields.DisplayOrder").Text,
                                    Width = "150",
                                    ClassName =  NopColumnClassDefaults.CenterAll
                                },
                                new ColumnProperty(nameof(CategoryModel.Id))
                                {
                                    Title = T("Admin.Common.Edit").Text,
                                    Width = "100",
                                    ClassName =  NopColumnClassDefaults.Button,
                                    Render = new RenderButtonEdit(new DataUrl("~/Admin/Category/Edit"))
                                }
                            }
                        })

                        <script>
                            $(function() {
                                $('#delete-selected-action-confirmation-submit-button').on('click', function () {
                                    var postData = {
                                        selectedIds: selectedIds
                                    };
                                    addAntiForgeryToken(postData);
                                    $.ajax({
                                        cache: false,
                                        type: "POST",
                                        url: "@(Url.Action("DeleteSelected", "Category"))",
                                        data: postData,
                                        error: function (jqXHR, textStatus, errorThrown) {
                                            showAlert('deleteSelectedFailed', errorThrown);
                                        },
                                        complete: function (jqXHR, textStatus) {
                                            if (jqXHR.status === 204)
                                            {
                                                showAlert('nothingSelectedAlert', '@T("Admin.Common.Alert.NothingSelected")');
                                                return;
                                            }
                                            updateTable('#categories-grid');
                                        }
                                    });
                                    $('#delete-selected-action-confirmation').modal('toggle');
                                    return false;
                                });
                            });
                        </script>
                        <nop-alert asp-alert-id="deleteSelectedFailed" />
                        <nop-alert asp-alert-id="nothingSelectedAlert" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

@*import categories form*@
<div id="importexcel-window" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="importexcel-window-title">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="importexcel-window-title">@T("Admin.Common.ImportFromExcel")</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>                
            </div>
            <form asp-controller="Category" asp-action="ImportFromXlsx" method="post" enctype="multipart/form-data">
                <div class="form-horizontal">
                    <div class="modal-body">
                        <ul class="common-list">
                            <li>
                                <em>@T("Admin.Catalog.Categories.List.ImportFromExcelTip")</em>
                            </li>
                            <li>
                                <em>@T("Admin.Common.ImportFromExcel.ManyRecordsWarning")</em>
                            </li>
                        </ul>
                        <div class="form-group row">
                            <div class="col-md-2">
                                <div class="label-wrapper">
                                    <label class="col-form-label">
                                        @T("Admin.Common.ExcelFile")
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-10">
                                <input type="file" id="importexcelfile" name="importexcelfile" class="form-control" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">
                            @T("Admin.Common.ImportFromExcel")
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<nop-action-confirmation asp-button-id="delete-selected" />
